package com.guet.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guet.common.utils.R;
import com.guet.main.entity.Room;
import com.guet.main.entity.RoomTimeSlice;
import com.guet.main.entity.User;
import com.guet.main.entity.vo.QuerySliceVo;
import com.guet.main.entity.vo.RoomTimeSliceVo;
import com.guet.main.service.RoomService;
import com.guet.main.service.RoomTimeSliceService;
import com.guet.main.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * room_time_slice 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-07-28
 */
@RestController
@RequestMapping("/main/room-time-slice")
@CrossOrigin
public class RoomTimeSliceController {
    @Autowired
    private RoomTimeSliceService roomTimeSliceService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private UserService userService;

    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        RoomTimeSlice byId = roomTimeSliceService.getById(id);
        return R.ok().data("slice",byId);
    }

    @ApiOperation("根据日期获取其所有的时间片")
    @GetMapping("/getByDate/{roomID}/{date}")
    //todo 不用了 使用getByQuery即可
    public R getByDate(@PathVariable String roomID,@PathVariable String date){
        QueryWrapper<RoomTimeSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomID);
        queryWrapper.like("slice_date",date);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String dateNow = format1.format(new Date());
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm");
        String time = format2.format(new Date());
        if(date.equals(dateNow)){
            queryWrapper.ge("begin_time",time);
        }
        List<RoomTimeSlice> list = roomTimeSliceService.list(queryWrapper);
        return R.ok().data("list",list);
    }

    @ApiOperation("根据会议室id获取其所有的时间片")
    @GetMapping("/getByRoomID/{roomID}")
    public R getByRoomID(@PathVariable String roomID){
        QueryWrapper<RoomTimeSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("slice_date+begin_time");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String date = format1.format(new Date());
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm");
        String time = format2.format(new Date());
        queryWrapper.or(wrapper1->wrapper1.gt("slice_date",date).and(queryWrapper3->queryWrapper3.eq("room_id",roomID)))
                .or(wrapper2->wrapper2.and(wrapper3->wrapper3.eq("slice_date",date).ge("begin_time",time)));
        queryWrapper.and(queryWrapper3->queryWrapper3.eq("room_id",roomID));
        List<RoomTimeSlice> list = roomTimeSliceService.list(queryWrapper);
        return R.ok().data("list",list);
    }

    @PostMapping("/add")
    @ApiOperation("增加时间片")
    public R add(@RequestBody RoomTimeSliceVo roomTimeSliceVo){
        // TODO: 2022/7/29 已经添加过的时间片不能再添加，即新的时间片不能和存在的时间片存在时间重叠，以及开始时间不能晚于结束时间
        RoomTimeSlice roomTimeSlice = new RoomTimeSlice();
        Room room = roomService.getById(roomTimeSliceVo.getRoomId());
        BeanUtils.copyProperties(roomTimeSliceVo,roomTimeSlice);
        roomTimeSlice.setRoomName(room.getName());
        boolean save = roomTimeSliceService.save(roomTimeSlice);
        return save?R.ok():R.error();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除时间片")
    public R delete(@PathVariable String id){
        boolean b = roomTimeSliceService.removeById(id);
        return b?R.ok():R.error();
    }

    @PutMapping("/update")
    @ApiOperation("更新时间片")
    public R update(@RequestBody RoomTimeSlice roomTimeSlice){
        boolean b = roomTimeSliceService.updateById(roomTimeSlice);
        return b?R.ok():R.error();
    }

    @PostMapping("/getByQuery")
    @ApiOperation("条件查询")
    public R getByQuery(@RequestBody QuerySliceVo querySliceVo){
        List<RoomTimeSlice> list = roomTimeSliceService.querySearch(querySliceVo);
        return R.ok().data("list",list);
    }

    @GetMapping("/list")
    @ApiOperation("获取所有的时间片")
    public R list(){
        List<RoomTimeSlice> list = roomTimeSliceService.list(null);
        QueryWrapper<RoomTimeSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("slice_date",new Date());
        return R.ok().data("list",list);
    }

    @PostMapping("/addByFile")
    @ApiOperation("增加时间片")
    public R add(MultipartFile file,String roomId){
        roomTimeSliceService.saveSlice(file,roomTimeSliceService,roomService,roomId);
        return R.ok();
    }
}

